/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.BowParamDao;
import com.taobao.snowsnow.hibernate.pojo.BowParam;
import com.taobao.snowsnow.hibernate.pojo.ProductCollect;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class BowParamDaoImpl extends BaseDaoImpl implements BowParamDao {
	
	public BowParam findById(int id) throws Exception {
		return (BowParam) getHibernateTemplate().get(BowParam.class, id);
	}
	
	public List<BowParam> findByName(String name) throws Exception {
		return super.findByProperty(BowParam.class, "name", name);
	}

	public List<BowParam> findAll() throws Exception {
		return getHibernateTemplate().find("from BowParam");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(BowParam.class);
	}

	public List<BowParam> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(BowParam.class, page, limit);
	}

	@Override
	public BowParam getFirstObject() throws Exception {
		Query query = getSession().createQuery("from BowParam as o where o.id = (select min(id) from BowParam)");
		List<BowParam> list = query.list();
		if(list.size() !=0)
			return (BowParam) list.get(0);
		else return null;
	}

	@Override
	public BowParam getLastObject() throws Exception {
		Query query = getSession().createQuery("from BowParam as o where o.id = (select max(id) from BowParam)");
		List<BowParam> list = query.list();
		if(list.size() !=0)
			return (BowParam) list.get(0);
		else return null;
	}

	@Override
	public BowParam findByNamePrecise(String name) throws Exception {
		List<BowParam> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}

	@Override
	public List<BowParam> findByProductCollect(ProductCollect productCollect) throws Exception {
		return getHibernateTemplate().find("from BowParam as o where o.productCollect = ?", productCollect);
	}
}
